<?php
class RoleAction extends CommAction{
	public function __construct(){
		parent::__construct();
        $menuid =!empty($_REQUEST['menuid'])?intval($_REQUEST['menuid']):'';
        $nav_submenu=$this->submenu($menuid,false,-1);
        $this->assign('nav_submenu',$nav_submenu);
	}
	/**
     * lujinfa
     * 2013-09-04
     * 添加角色往
	*/
     public function add(){

		if($_POST['dosubmit']){
			$role = new RoleModel('role');

			$data['name'] = trim($_POST['rolename']);
			$data['remark'] 	= trim($_POST['roledesc']);
			$data['status'] 	= trim($_POST['rolestatus']);
			//$data['roleaddtime'] = time();
			$roleid = $role->add($data);
			if($roleid){
				$this->success('添加成功');

			}else{
				$this->error('添加失败，请重新添加','__URL__/addrole');
			}
		}else{
			$this->display('addrole');
		}
	}

    /**
     * lujifna
     * 2013-09-04
     * 角色列表
     */
    public function index(){
		import('@.ORG.Util.Page');
		$role = new Model('role');
		$condition['id']= array('neq',0);
		$role_count = $role->where($condition)->count();
		$role_page = new Page($role_count,15);
		$role_show = $role_page ->show();
		$rolelist = $role->field('id,name,remark,status,pid')->where($condition)->limit($role_page->firstRow.','.$role_page->listRows)->ORDER('id ASC')->select();

        $admin_db = new Model('admin');
        $condition_admin['adminid']=session('userid');
        $local_admin = $admin_db->field('*')->where($condition_admin)->find();


        $this->assign('local_admin',$local_admin);
        $this->assign('rolelist',$rolelist);
		$this->assign('rolepage',$role_show);
		$this->display('rolelist');
	}

	/**
     * lujinfa
     * 2013-09-04
	 * 设置角色权限
	 */
	public  function  setting_priv(){

        $roleid = !empty($_REQUEST['roleId'])?intval(trim($_REQUEST['roleId'])):0;

        if($roleid<=0)
        {
            $this->error('传值失败');
        }
        $admin_db = new Model('admin');
        $condition_admin['adminid']=session('userid');
        $local_admin = $admin_db->field('*')->where($condition_admin)->find();

        if($local_admin['roleid']!=1 && $roleid==1)
        {
            $this->error('非超级管理员，无权修改“超级管理员角色”');
        }

		if(isset($_POST['dosubmit'])){
			if (is_array($_POST['menuid']) && count($_POST['menuid']) > 0) {

				$accessModel = new Model('access');
				$moduleModel = new ModuleModel('node');
				$con['role_id'] = $roleid;
				$accessModel->where($con)->delete();
				foreach($_POST['menuid'] as $menuid){
					$info = array();
					$condition['id'] = $menuid;
					$info = $moduleModel->where($condition)->find();
					$data['role_id'] = $roleid;
					$data['node_id'] = $info['id'];
					$data['level'] = $info['level'];
					$data['pid'] = $info['pid'];
					$roleid = $accessModel->add($data);
				}
				$this->success('设置成功','__URL__/rolelist');
			}else{
				$this->error('设置失败，请重新设置','__URL__/setNode/roleId/'.$roleid);
			}
		}else {
			$roleid = intval($_GET['roleId']);
            import("@.ORG.Util.Tree");
			$tree = new Tree();
			$tree->icon = array('│ ','├─ ','└─ ');
			$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
			$moduleModel = new Model('node');
			$accessModel = new Model('access');
			$result = $moduleModel->select();
			$modules = 'admin,announce,vote,system';
			foreach ($result as $n=>$t) {
				$t['cname'] = $t['title'];
				$con['role_id'] = $roleid;
				$con['node_id'] = $t['id'];
				$re = $accessModel->where($con)->find();
				if($re){
					$t['checked']  = "checked=''";
				}else{
					$t['checked'] = "";
				}				
				$t['parentid_node'] = ($t['pid'])? ' class="child-of-node-'.$t['pid'].'"' : '';
				$array[] = $t;
			}
			$str  = "<tr id='node-\$id' \$parentid_node>
							<td style='padding-left:30px;'>\$spacer<input type='checkbox' name='menuid[]' value='\$id' level='\$level' \$checked onclick='javascript:checknode(this);'> \$cname</td>
						</tr>";

			$tree->init($array);
			$categorys = $tree->get_tree(0, $str);
			$this->assign('categorys',$categorys);
			$this->assign('roleid',$roleid);
			$this->display('role_node');
		}

	}
	
	//更新角色
	public function edit(){
		if($_POST['dosubmit']){
			$data['name'] = trim($_POST['rolename']);
			$data['remark'] = trim($_POST['roledesc']);
			$data['status'] = trim($_POST['rolestatus']);
			//$data['roleaddtime'] = trim($_POST['roleaddtime']);
			$condition2['id'] = $_POST['id'];
			$role2 = new Model('role');
			
			if($role2->where($condition2)->save($data)){
				$this->success('更新成功','__URL__/rolelist');

			}else{
				$this->error('更新失败','__URL__/rolelist');
			}
		}else{
			$condition['id'] = trim($_GET['id']);
			$role = new Model('role');
			$rolemessage = $role->field('id,name,remark,status,pid')->where($condition)->find();
			//print_r ($rolemessage);
			$this->assign('rolemessage',$rolemessage);
			$this->display('updaterole');
		}

	}

    /**
     * lujinfa
     * 2013-09-04
     * 删除角色
     */
    public function delete(){
		$condition['id'] = $_GET['id'];
		$role = new Model('role');
		
		$roleid = $role->where($condition)->getField('id');
		$roleuser = new Model('admin');
		$condition2['roleid'] = $_GET['id'];
		$roleuserid = $roleuser->where($condition2)->getField('roleid');
		if($roleid == $roleuserid){
			$this->error('该角色下有管理员,不能删除','__URL__/rolelist');
		}
		else{
		if(!empty($roleid)){
			if($role->where($condition)->delete()){
				$this->success('删除成功','__URL__/rolelist');
			}else{
				$this->error('删除信息失败','__URL__/rolelist');
			}
		}else{
			$this->error('删除角色不存在');
		}
	}
	}
}
?>